﻿using System;
using System.Collections.Generic;

using System.Text;
using RebEngine.Managers;
using TomShane.Neoforce.Controls;
using Microsoft.Xna.Framework.Graphics;
using RebellionII.GameObjects.Map;
using RebellionII.GUI.Parts;
using RebellionII.GameObjects.Units;
using RebellionII.GameObjects;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace RebellionII.GUI
{
    public class BuildWindow : Window
    {
        RebSystem selectedSystem;
        RebUnitPanel UnitPanel;

        Dictionary<int, int> troopDictionary = new Dictionary<int, int>();
        Dictionary<int, int> fighterDictionary = new Dictionary<int, int>();
        Dictionary<int, int> charactersDictionary = new Dictionary<int, int>();
        Dictionary<int, int> facilityDictionary = new Dictionary<int, int>();
        int shipbuild = 0; int troopbuild = 0; int facbuild = 0;
        #region TabPage0
        private GroupPanel pnlFacilityBuild;
        private Label lblFacilityNumber;
        private Label lblFacilityDestination;
        private Label ulblFacilityDestination;
        private ImageBox imgFacilityCurrentBuild;
        private ProgressBar progbarFacilityCurrentBuild;
        private Label ulblfacNum;
        private Label lblNameFacilityBuild;
        private GroupPanel pnlTroopBuild;
        private Label lblNumTroopsTrained;
        private Label lblDestinationTroopTrained;
        private Label ulblTroopDest;
        private ImageBox imgTroopBuild;
        private ProgressBar progbarTroopBuild;
        private Label ulblTroopTrain;
        private Label lblNameTroopTrained;
        private GroupPanel pnlShipBuild;
        private Label lblNumShipsBuild;
        private Label lblDestinationShipBuild;
        private Label ulblDestShip;
        private ImageBox imgShipBuild;
        private ProgressBar progbarShipBuild;
        private Label ulblShipNum;
        private Label lblNameOfBuild;
        private ImageBox imgFacility;
        private ImageBox imgTroop;
        private ImageBox imgShipyard;
        #endregion

        private TabControl tabControl1;

        /// <summary>
        /// The Build Window GUI. Used to show off the units in a system and allow building/destruction of units.
        /// </summary>
        /// <param name="system">The System you wish to display.</param>
        public BuildWindow(RebSystem system)
            :base(InputManager.Manager)
        {
            selectedSystem = system;

            Init();
            Center();
            Height = 395;
            Width = 375;
            Name = system.sName + " Main Dialog";
            Text = system.sName;
            Closed += new WindowClosedEventHandler(thisClosed);
            InputManager.Manager.Add(this);
            AutoScroll = false;

            #region Init Dictionaries
            foreach (var temp in selectedSystem.lTroop)
            {
                if (!troopDictionary.ContainsKey(temp.GetReferenceID))
                {
                    troopDictionary.Add(temp.GetReferenceID, 1);
                }
                else
                {
                    troopDictionary[temp.GetReferenceID]++;
                }
            }

            foreach (var temp in selectedSystem.lFighter)
            {
                if (!fighterDictionary.ContainsKey(temp.GetReferenceID))
                {
                    fighterDictionary.Add(temp.GetReferenceID, 1);
                }
                else
                {
                    fighterDictionary[temp.GetReferenceID]++;
                }
            }

            foreach (var temp in selectedSystem.lFacilities)
            {
                if (!facilityDictionary.ContainsKey(temp.iReferenceID))
                {
                    facilityDictionary.Add(temp.iReferenceID, 1);
                }
                else
                {
                    facilityDictionary[temp.iReferenceID]++;
                }
            }
            #endregion

            tabControl1 = new TabControl(InputManager.Manager);
            tabControl1.Init();
            tabControl1.Left = 2;
            tabControl1.Top = 3;
            tabControl1.Width = 373;
            tabControl1.Height = 400;
            tabControl1.PageChanged += new TomShane.Neoforce.Controls.EventHandler(tabControl1_PageChanged);
            tabControl1.Text = "";
            tabControl1.AddPage();
            tabControl1.TabPages[0].Text = "Overview";
            tabControl1.AddPage();
            tabControl1.TabPages[1].Text = "Characters";
            tabControl1.AddPage();
            tabControl1.TabPages[2].Text = "Troops";
            tabControl1.AddPage();
            tabControl1.TabPages[3].Text = "Fighters";
            tabControl1.AddPage();
            tabControl1.TabPages[4].Text = "Facilities";

            this.Add(tabControl1);

            UnitPanel = new RebUnitPanel(this, 200, 25);
            UnitPanel.GroupPanel.Visible = false;

            #region TabPage 0 - Overview

            ImageBox backingImage = new ImageBox(InputManager.Manager);
            backingImage.Init();
            backingImage.Left = 0; backingImage.Top = 0;
            backingImage.Width = tabControl1.TabPages[0].Width;
            backingImage.Height = tabControl1.TabPages[0].Height;
            backingImage.Alpha = 100;
            backingImage.Image = TextureManager.GetTexture("backing").BaseTexture as Texture2D;
            backingImage.SizeMode = SizeMode.Stretched;
            tabControl1.TabPages[0].Add(backingImage);

            #region ShipPart
            pnlShipBuild = new GroupPanel(InputManager.Manager);
            pnlShipBuild.Init();
            pnlShipBuild.Left = 108;
            pnlShipBuild.Top = 5;
            pnlShipBuild.Width = 245;
            pnlShipBuild.Height = 80;
            pnlShipBuild.Text = "Ship Construction";
            pnlShipBuild.Color = system.TeamColor;
            tabControl1.TabPages[0].Add(pnlShipBuild);

            lblNumShipsBuild = new Label(InputManager.Manager);
            lblNumShipsBuild.Init();
            lblNumShipsBuild.Left = 90;
            lblNumShipsBuild.Top = 20;
            lblNumShipsBuild.Width = 40;
            lblNumShipsBuild.Height = 13;
            lblNumShipsBuild.Text = system.ShipBuildQuantity.ToString();
            pnlShipBuild.Add(lblNumShipsBuild);


            lblDestinationShipBuild = new Label(InputManager.Manager);
            lblDestinationShipBuild.Init();
            lblDestinationShipBuild.Left = 90;
            lblDestinationShipBuild.Top = 35;
            lblDestinationShipBuild.Width = 120;
            lblDestinationShipBuild.Height = 13;
            if (system.ShipBuildDestinationID != 0)
                lblDestinationShipBuild.Text = NetClientManager.gameUniverse.dUniverse[system.ShipBuildDestinationID].sName;
            else
                lblDestinationShipBuild.Text = "";
            pnlShipBuild.Add(lblDestinationShipBuild);

            ulblDestShip = new Label(InputManager.Manager);
            ulblDestShip.Init();
            ulblDestShip.Left = 3;
            ulblDestShip.Top = 35;
            ulblDestShip.Width = 90;
            ulblDestShip.Height = 13;
            ulblDestShip.Text = "Destination:";
            pnlShipBuild.Add(ulblDestShip);

            string ship = null;
            int currentShipProg = 0;
            int shiptotalProg = 1;
            if (system.ShipBuildReferenceID != 0)
            {
                ship = NetClientManager.UnitList[system.ShipBuildReferenceID].sTextureName;
                currentShipProg = system.ShipBuildCurrentProgress;
                shiptotalProg = NetClientManager.UnitList[system.ShipBuildReferenceID].iConstructionCost;
            }
            imgShipBuild = new ImageBox(InputManager.Manager);
            imgShipBuild.Init();
            imgShipBuild.Left = 150;
            imgShipBuild.Top = 3;
            imgShipBuild.Width = 80;
            imgShipBuild.Height = 40;
            imgShipBuild.Text = "";
            if (ship != null)
                imgShipBuild.Image = TextureManager.GetTexture(ship).BaseTexture as Texture2D;
            else
                imgShipBuild.Image = TextureManager.GetTexture("Clear").BaseTexture as Texture2D;
            imgShipBuild.SizeMode = SizeMode.Stretched;
            imgShipBuild.Color = new Color(255, 255, 255, 150);
            pnlShipBuild.Add(imgShipBuild);

            progbarShipBuild = new ProgressBar(InputManager.Manager);
            progbarShipBuild.Init();
            progbarShipBuild.Left = 0;
            progbarShipBuild.Top = 50;
            progbarShipBuild.Width = pnlShipBuild.Width;
            progbarShipBuild.Height = 7;
            progbarShipBuild.Text = "";
            //progbarShipBuild.Mode = ProgressBarMode.Default;
            //progbarShipBuild.Passive = false;
            if (ship != null)
            {
                progbarShipBuild.Range = shiptotalProg;
                progbarShipBuild.Value = currentShipProg;
            }
            else
            {
                progbarShipBuild.Range = 1;
                progbarShipBuild.Value = 0;
            }
            pnlShipBuild.Add(progbarShipBuild);

            ulblShipNum = new Label(InputManager.Manager);
            ulblShipNum.Init();
            ulblShipNum.Left = 3;
            ulblShipNum.Top = 20;
            ulblShipNum.Width = 90;
            ulblShipNum.Height = 13;
            ulblShipNum.Text = "Quantity:";
            pnlShipBuild.Add(ulblShipNum);


            lblNameOfBuild = new Label(InputManager.Manager);
            lblNameOfBuild.Init();
            lblNameOfBuild.Left = 3;
            lblNameOfBuild.Top = 3;
            lblNameOfBuild.Width = 200;
            lblNameOfBuild.Height = 13;
            if (system.ShipBuildQuantity == 0)
            {
                lblNameOfBuild.Text = "No Ships Being Built";
            }
            else
            {
                lblNameOfBuild.Text = NetClientManager.UnitList[system.ShipBuildReferenceID].sName;
            }
            pnlShipBuild.Add(lblNameOfBuild);
            #endregion

            #region TroopPart
            pnlTroopBuild = new GroupPanel(InputManager.Manager);
            pnlTroopBuild.Init();
            pnlTroopBuild.Left = 108;
            pnlTroopBuild.Top = 90;
            pnlTroopBuild.Width = 245;
            pnlTroopBuild.Height = 80;
            pnlTroopBuild.Color = system.TeamColor;
            pnlTroopBuild.Text = "Troop Construction";
            tabControl1.TabPages[0].Add(pnlTroopBuild);

            ulblTroopTrain = new Label(InputManager.Manager);
            ulblTroopTrain.Init();
            ulblTroopTrain.Left = 3;
            ulblTroopTrain.Top = 20;
            ulblTroopTrain.Width = 90;
            ulblTroopTrain.Height = 13;
            ulblTroopTrain.Text = "Quantity:";
            pnlTroopBuild.Add(ulblTroopTrain);

            ulblTroopDest = new Label(InputManager.Manager);
            ulblTroopDest.Init();
            ulblTroopDest.Left = 3;
            ulblTroopDest.Top = 35;
            ulblTroopDest.Width = 90;
            ulblTroopDest.Height = 13;
            ulblTroopDest.Text = "Destination:";
            pnlTroopBuild.Add(ulblTroopDest);

            lblNumTroopsTrained = new Label(InputManager.Manager);
            lblNumTroopsTrained.Init();
            lblNumTroopsTrained.Left = 90;
            lblNumTroopsTrained.Top = 20;
            lblNumTroopsTrained.Width = 40;
            lblNumTroopsTrained.Height = 13;
            lblNumTroopsTrained.Text = system.TroopBuildQuantity.ToString();
            pnlTroopBuild.Add(lblNumTroopsTrained);

            lblDestinationTroopTrained = new Label(InputManager.Manager);
            lblDestinationTroopTrained.Init();
            lblDestinationTroopTrained.Left = 90;
            lblDestinationTroopTrained.Top = 35;
            lblDestinationTroopTrained.Width = 120;
            lblDestinationTroopTrained.Height = 13;
            if (system.TroopBuildDestinationID == 0)
                lblDestinationTroopTrained.Text = "";
            else
                lblDestinationTroopTrained.Text = NetClientManager.gameUniverse.dUniverse[system.TroopBuildDestinationID].sName;
            pnlTroopBuild.Add(lblDestinationTroopTrained);



            string troop = null;
            int currentTroopProg = 0;
            int troopTotalProg = 1;
            if (system.TroopBuildReferenceID != 0)
            {
                troop = NetClientManager.UnitList[system.TroopBuildReferenceID].sTextureName;
                currentTroopProg = system.TroopBuildCurrentProgress;
                troopTotalProg = NetClientManager.UnitList[system.TroopBuildReferenceID].iConstructionCost;
            }
            imgTroopBuild = new ImageBox(InputManager.Manager);
            imgTroopBuild.Init();
            imgTroopBuild.Left = 150;
            imgTroopBuild.Top = 3;
            imgTroopBuild.Width = 80;
            imgTroopBuild.Height = 40;
            imgTroopBuild.Text = "";
            if (troop != null)
                imgTroopBuild.Image = TextureManager.GetTexture(troop).BaseTexture as Texture2D;
            else
                imgTroopBuild.Image = TextureManager.GetTexture("Clear").BaseTexture as Texture2D;

            imgTroopBuild.SizeMode = SizeMode.Stretched;
            imgTroopBuild.Color = new Color(255, 255, 255, 150);
            pnlTroopBuild.Add(imgTroopBuild);

            progbarTroopBuild = new ProgressBar(InputManager.Manager);
            progbarTroopBuild.Init();
            progbarTroopBuild.Left = 0;
            progbarTroopBuild.Top = 50;
            progbarTroopBuild.Width = pnlTroopBuild.Width;
            progbarTroopBuild.Height = 7;
            progbarTroopBuild.Text = "";
            pnlTroopBuild.Add(progbarTroopBuild);
            if (troop != null)
            {
                progbarTroopBuild.Range = troopTotalProg;
                progbarTroopBuild.Value = currentTroopProg;
            }
            else
            {
                progbarTroopBuild.Range = 0;
                progbarTroopBuild.Value = 0;
            }

            lblNameTroopTrained = new Label(InputManager.Manager);
            lblNameTroopTrained.Init();
            lblNameTroopTrained.Left = 3;
            lblNameTroopTrained.Top = 3;
            lblNameTroopTrained.Width = 200;
            lblNameTroopTrained.Height = 13;
            if (system.TroopBuildQuantity == 0)
                lblNameTroopTrained.Text = "No Troops Being Trained";
            else
                lblNameTroopTrained.Text = NetClientManager.UnitList[system.TroopBuildReferenceID].sName;
            pnlTroopBuild.Add(lblNameTroopTrained);
            #endregion

            #region FacPart
            pnlFacilityBuild = new GroupPanel(InputManager.Manager);
            pnlFacilityBuild.Init();
            pnlFacilityBuild.Left = 108;
            pnlFacilityBuild.Top = 175;
            pnlFacilityBuild.Width = 245;
            pnlFacilityBuild.Height = 80;
            pnlFacilityBuild.Text = "Facility Construction:";
            pnlFacilityBuild.Color = system.TeamColor;
            tabControl1.TabPages[0].Add(pnlFacilityBuild);

            ulblfacNum = new Label(InputManager.Manager);
            ulblfacNum.Init();
            ulblfacNum.Left = 3;
            ulblfacNum.Top = 20;
            ulblfacNum.Width = 90;
            ulblfacNum.Height = 13;
            ulblfacNum.Text = "Quantity:";
            pnlFacilityBuild.Add(ulblfacNum);

            lblNameFacilityBuild = new Label(InputManager.Manager);
            lblNameFacilityBuild.Init();
            lblNameFacilityBuild.Left = 3;
            lblNameFacilityBuild.Top = 3;
            lblNameFacilityBuild.Width = 200;
            lblNameFacilityBuild.Height = 13;
            if (system.FacilityBuildQuantity != 0)
                lblNameFacilityBuild.Text = "";
            else
                lblNameFacilityBuild.Text = "No Facilities Being Built";
            pnlFacilityBuild.Add(lblNameFacilityBuild);
            
            ulblFacilityDestination = new Label(InputManager.Manager);
            ulblFacilityDestination.Init();
            ulblFacilityDestination.Left = 3;
            ulblFacilityDestination.Top = 35;
            ulblFacilityDestination.Width = 90;
            ulblFacilityDestination.Height = 13;
            ulblFacilityDestination.Text = "Destination:";
            pnlFacilityBuild.Add(ulblFacilityDestination);

            lblFacilityDestination = new Label(InputManager.Manager);
            lblFacilityDestination.Init();
            lblFacilityDestination.Left = 90;
            lblFacilityDestination.Top = 35;
            lblFacilityDestination.Width = 120;
            lblFacilityDestination.Height = 13;
            if (system.FacilityBuildDestinationID != 0)
            {
                lblFacilityDestination.Text = NetClientManager.gameUniverse.dUniverse[selectedSystem.FacilityBuildDestinationID].sName;
            }
            else
            {
                lblFacilityDestination.Text = "";
            }
            pnlFacilityBuild.Add(lblFacilityDestination);

            lblFacilityNumber = new Label(InputManager.Manager);
            lblFacilityNumber.Init();
            lblFacilityNumber.Left = 90;
            lblFacilityNumber.Top = 20;
            lblFacilityNumber.Width = 40;
            lblFacilityNumber.Height = 13;
            lblFacilityNumber.Text = system.FacilityBuildQuantity.ToString();
            pnlFacilityBuild.Add(lblFacilityNumber);

            string fac = null;
            int currentfacProg = 0; 
            int facTotalProg = 1;
            if (system.FacilityBuildReferenceID != 0)
            {
                fac = NetClientManager.UnitList[system.FacilityBuildReferenceID].sTextureName;
                currentfacProg = system.FacilityBuildCurrentProgress;
                facTotalProg = NetClientManager.UnitList[system.FacilityBuildReferenceID].iConstructionCost;
            } 
            imgFacilityCurrentBuild = new ImageBox(InputManager.Manager);
            imgFacilityCurrentBuild.Init();
            imgFacilityCurrentBuild.Left = 150;
            imgFacilityCurrentBuild.Top = 3;
            imgFacilityCurrentBuild.Width = 80;
            imgFacilityCurrentBuild.Height = 40;
            imgFacilityCurrentBuild.Text = "";
            if (fac != null)
                imgFacilityCurrentBuild.Image = TextureManager.GetTexture(fac).BaseTexture as Texture2D;
            else
                imgFacilityCurrentBuild.Image = TextureManager.GetTexture("Clear").BaseTexture as Texture2D;
            imgFacilityCurrentBuild.SizeMode = SizeMode.Stretched;
            imgFacilityCurrentBuild.Alpha = 200;
            imgFacilityCurrentBuild.Color = new Color(255, 255, 255, 150);
            pnlFacilityBuild.Add(imgFacilityCurrentBuild);
            

            progbarFacilityCurrentBuild = new ProgressBar(InputManager.Manager);
            progbarFacilityCurrentBuild.Init();
            progbarFacilityCurrentBuild.Left = 0;
            progbarFacilityCurrentBuild.Top = 50;
            progbarFacilityCurrentBuild.Width = pnlFacilityBuild.Width;
            progbarFacilityCurrentBuild.Height = 7;
            progbarFacilityCurrentBuild.Text = "";
            if (fac != null)
            {
                progbarFacilityCurrentBuild.Range = facTotalProg;
                progbarFacilityCurrentBuild.Value = currentfacProg;
            }
            else
            {
                progbarFacilityCurrentBuild.Range = 0;
                progbarFacilityCurrentBuild.Value = 0;
            }
            pnlFacilityBuild.Add(progbarFacilityCurrentBuild);

            #endregion

            foreach (var temp in system.lFacilities)
            {
                if ((NetClientManager.UnitList[temp.GetReferenceID] as Facility).eType == FacilityType.Ships)
                    shipbuild++;
                if ((NetClientManager.UnitList[temp.GetReferenceID] as Facility).eType == FacilityType.Troops)
                    troopbuild++;
                if ((NetClientManager.UnitList[temp.GetReferenceID] as Facility).eType == FacilityType.Facilities)
                    facbuild++;
            }
                    
            #region PanelImages
            #region Ship
            Panel pnlShipImage = new Panel(InputManager.Manager);
            pnlShipImage.Init();
            pnlShipImage.Left = 5;
            pnlShipImage.Top = 5;
            pnlShipImage.Width = 100;
            pnlShipImage.Height = 59;
            pnlShipImage.ContextMenu = new ContextMenu(InputManager.Manager);
            pnlShipImage.ContextMenu.Init();
            pnlShipImage.ContextMenu.Items.Add(new MenuItem("Build"));
            pnlShipImage.ContextMenu.Items.Add(new MenuItem("Stop Current"));
            pnlShipImage.ContextMenu.Items[0].Click += new TomShane.Neoforce.Controls.EventHandler(ShipBuild);
            pnlShipImage.ContextMenu.Items[1].Click += new TomShane.Neoforce.Controls.EventHandler(ShipCancel);
            if (shipbuild > 0)
            {
                pnlShipImage.ContextMenu.Items[0].Enabled = true;
                pnlShipImage.ContextMenu.Items[1].Enabled = true;
            }
            else
            {
                pnlShipImage.ContextMenu.Items[0].Enabled = false;
                pnlShipImage.ContextMenu.Items[1].Enabled = false;
            }
            tabControl1.TabPages[0].Add(pnlShipImage);


            
            imgShipyard = new ImageBox(InputManager.Manager);
            imgShipyard.Init();
            imgShipyard.Left = 4;
            imgShipyard.Top = 4;
            imgShipyard.Width = 92;
            imgShipyard.Height = 42;
            imgShipyard.Text = "Shipyards";
            imgShipyard.Image = TextureManager.GetTexture("shipyard").BaseTexture as Texture2D;
            imgShipyard.SizeMode = SizeMode.Stretched;
            imgShipyard.ContextMenu = new ContextMenu(InputManager.Manager);
            imgShipyard.ContextMenu.Init();
            imgShipyard.ContextMenu.Items.Add(new MenuItem("Build"));
            imgShipyard.ContextMenu.Items.Add(new MenuItem("Stop Current"));
            imgShipyard.ContextMenu.Items[0].Click += new TomShane.Neoforce.Controls.EventHandler(ShipBuild);
            imgShipyard.ContextMenu.Items[1].Click += new TomShane.Neoforce.Controls.EventHandler(ShipCancel);
            if (shipbuild > 0)
            {
                imgShipyard.ContextMenu.Items[0].Enabled = true;
                imgShipyard.ContextMenu.Items[1].Enabled = true;
            }
            else
            {
                imgShipyard.ContextMenu.Items[0].Enabled = false;
                imgShipyard.ContextMenu.Items[1].Enabled = false;
            }
            pnlShipImage.Add(imgShipyard);

            Panel numOfShipyards = new Panel(InputManager.Manager);
            numOfShipyards.Init();
            numOfShipyards.Left = 5;
            numOfShipyards.Top = 65;
            numOfShipyards.Width = 100;
            numOfShipyards.Height = 20;
            tabControl1.TabPages[0].Add(numOfShipyards);

            Label lblNumShipyards = new Label(InputManager.Manager);
            lblNumShipyards.Init();
            lblNumShipyards.Left = 49;
            lblNumShipyards.Top = 5;
            lblNumShipyards.Width = 40;
            lblNumShipyards.Height = 10;
            lblNumShipyards.Text = shipbuild.ToString();
            numOfShipyards.Add(lblNumShipyards);
            #endregion

            #region Troop
            Panel pnlTroopImage = new Panel(InputManager.Manager);
            pnlTroopImage.Init();
            pnlTroopImage.Left = 5;
            pnlTroopImage.Top = 90;
            pnlTroopImage.Width = 100;
            pnlTroopImage.Height = 59;
            pnlTroopImage.ContextMenu = new ContextMenu(InputManager.Manager);
            pnlTroopImage.ContextMenu.Init();
            pnlTroopImage.ContextMenu.Items.Add(new MenuItem("Build"));
            pnlTroopImage.ContextMenu.Items.Add(new MenuItem("Stop Current"));
            pnlTroopImage.ContextMenu.Items[0].Click += new TomShane.Neoforce.Controls.EventHandler(TroopBuild);
            pnlTroopImage.ContextMenu.Items[1].Click += new TomShane.Neoforce.Controls.EventHandler(TroopCancel);
            if (troopbuild > 0)
            {
                pnlTroopImage.ContextMenu.Items[0].Enabled = true;
                pnlTroopImage.ContextMenu.Items[1].Enabled = true;
            }
            else
            {
                pnlTroopImage.ContextMenu.Items[0].Enabled = false;
                pnlTroopImage.ContextMenu.Items[1].Enabled = false;
            }
            tabControl1.TabPages[0].Add(pnlTroopImage);

            imgTroop = new ImageBox(InputManager.Manager);
            imgTroop.Init();
            imgTroop.Left = 4;
            imgTroop.Top = 4;
            imgTroop.Width = 92;
            imgTroop.Height = 42;
            imgTroop.Text = "Troop Barracks";
            imgTroop.Image = TextureManager.GetTexture("troopyard").BaseTexture as Texture2D;
            imgTroop.SizeMode = SizeMode.Stretched;
            imgTroop.ContextMenu = new ContextMenu(InputManager.Manager);
            imgTroop.ContextMenu.Init();
            imgTroop.ContextMenu.Items.Add(new MenuItem("Build"));
            imgTroop.ContextMenu.Items.Add(new MenuItem("Stop Current"));
            imgTroop.ContextMenu.Items[0].Click += new TomShane.Neoforce.Controls.EventHandler(TroopBuild);
            imgTroop.ContextMenu.Items[1].Click += new TomShane.Neoforce.Controls.EventHandler(TroopCancel);
            if (troopbuild > 0)
            {
                imgTroop.ContextMenu.Items[0].Enabled = true;
                imgTroop.ContextMenu.Items[1].Enabled = true;
            }
            else
            {
                imgTroop.ContextMenu.Items[0].Enabled = false;
                imgTroop.ContextMenu.Items[1].Enabled = false;
            }
            pnlTroopImage.Add(imgTroop);

            Panel numOfTroopyards = new Panel(InputManager.Manager);
            numOfTroopyards.Init();
            numOfTroopyards.Left = 5;
            numOfTroopyards.Top = 150;
            numOfTroopyards.Width = 100;
            numOfTroopyards.Height = 20;
            tabControl1.TabPages[0].Add(numOfTroopyards);

            Label lblNumTroopyards = new Label(InputManager.Manager);
            lblNumTroopyards.Init();
            lblNumTroopyards.Left = 49;
            lblNumTroopyards.Top = 5;
            lblNumTroopyards.Width = 40;
            lblNumTroopyards.Height = 10;
            lblNumTroopyards.Text = troopbuild.ToString();
            numOfTroopyards.Add(lblNumTroopyards);

            #endregion

            #region Facility
            Panel pnlFacImage = new Panel(InputManager.Manager);
            pnlFacImage.Init();
            pnlFacImage.Left = 5;
            pnlFacImage.Top = 175;
            pnlFacImage.Width = 100;
            pnlFacImage.Height = 59;
            pnlFacImage.ContextMenu = new ContextMenu(InputManager.Manager);
            pnlFacImage.ContextMenu.Init();
            pnlFacImage.ContextMenu.Items.Add(new MenuItem("Build"));
            pnlFacImage.ContextMenu.Items.Add(new MenuItem("Destination"));
            pnlFacImage.ContextMenu.Items.Add(new MenuItem("Stop Current"));
            pnlFacImage.ContextMenu.Items[0].Click += new TomShane.Neoforce.Controls.EventHandler(FacilityBuild);
            pnlFacImage.ContextMenu.Items[1].Click += new TomShane.Neoforce.Controls.EventHandler(NewDestination);
            pnlFacImage.ContextMenu.Items[2].Click += new TomShane.Neoforce.Controls.EventHandler(FacilityCancel);
            if (facbuild > 0)
            {
                pnlFacImage.ContextMenu.Items[0].Enabled = true;
                pnlFacImage.ContextMenu.Items[1].Enabled = true;
                pnlFacImage.ContextMenu.Items[2].Enabled = true;
            }
            else
            {
                pnlFacImage.ContextMenu.Items[0].Enabled = false;
                pnlFacImage.ContextMenu.Items[1].Enabled = false;
                pnlFacImage.ContextMenu.Items[2].Enabled = false;
            }
            tabControl1.TabPages[0].Add(pnlFacImage);

            imgFacility = new ImageBox(InputManager.Manager);
            imgFacility.Init();
            imgFacility.Left = 4;
            imgFacility.Top = 4;
            imgFacility.Width = 92;
            imgFacility.Height = 42;
            imgFacility.Text = "Construction Yards";
            imgFacility.Image = TextureManager.GetTexture("constyard").BaseTexture as Texture2D;
            imgFacility.ContextMenu = new ContextMenu(InputManager.Manager);
            imgFacility.ContextMenu.Init();
            imgFacility.ContextMenu.Items.Add(new MenuItem("Build"));
            imgFacility.ContextMenu.Items.Add(new MenuItem("Destination"));
            imgFacility.ContextMenu.Items.Add(new MenuItem("Stop Current"));
            imgFacility.ContextMenu.Items[0].Click += new TomShane.Neoforce.Controls.EventHandler(FacilityBuild);
            imgFacility.ContextMenu.Items[1].Click += new TomShane.Neoforce.Controls.EventHandler(NewDestination);
            imgFacility.ContextMenu.Items[2].Click += new TomShane.Neoforce.Controls.EventHandler(FacilityCancel);
            if (facbuild > 0)
            {
                imgFacility.ContextMenu.Items[0].Enabled = true;
                imgFacility.ContextMenu.Items[1].Enabled = true;
                imgFacility.ContextMenu.Items[2].Enabled = true;
            }
            else
            {
                imgFacility.ContextMenu.Items[0].Enabled = false;
                imgFacility.ContextMenu.Items[1].Enabled = false;
                imgFacility.ContextMenu.Items[2].Enabled = false;
            }
            imgFacility.SizeMode = SizeMode.Stretched;
            pnlFacImage.Add(imgFacility);

            Panel numOfFacilitys = new Panel(InputManager.Manager);
            numOfFacilitys.Init();
            numOfFacilitys.Left = 5;
            numOfFacilitys.Top = 235;
            numOfFacilitys.Width = 100;
            numOfFacilitys.Height = 20;
            tabControl1.TabPages[0].Add(numOfFacilitys);

            Label lblnumOfFacilitys = new Label(InputManager.Manager);
            lblnumOfFacilitys.Init();
            lblnumOfFacilitys.Left = 49;
            lblnumOfFacilitys.Top = 5;
            lblnumOfFacilitys.Width = 40;
            lblnumOfFacilitys.Height = 10;
            lblnumOfFacilitys.Text = facbuild.ToString();
            numOfFacilitys.Add(lblnumOfFacilitys);
            #endregion

            #endregion

            #endregion

            #region TabPage 1 - Characters


            #endregion

            #region TabPage 2 - Troops
            int count = 0;

            foreach (var unit in troopDictionary)
            {
                RebUnitMoveScrap newMoveScrap = new RebUnitMoveScrap(tabControl1.TabPages[2], 3, 5 + 68 * count, unit.Value, unit.Key, selectedSystem);
                count++;
            }
            #endregion

            #region TabPage 3 - Fighters
            count = 0;

            foreach (var unit in fighterDictionary)
            {
                RebUnitMoveScrap newMoveScrap = new RebUnitMoveScrap(tabControl1.TabPages[3], 3, 5 + 68 * count, unit.Value, unit.Key, selectedSystem);
                count++;
            }
            #endregion

            #region TabPage 4 - Facilities
            count = 0;

            foreach (var unit in facilityDictionary)
            {
                RebUnitMoveScrap newMoveScrap = new RebUnitMoveScrap(tabControl1.TabPages[4], 3, 5 + 68 * count, unit.Value, unit.Key, selectedSystem);
                count++;
            }
            #endregion
        }

        void NewDestination(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            throw new NotImplementedException();
        }

        void thisClosed(object sender, WindowClosedEventArgs e)
        {
            InputManager.Manager.Remove(this);
        }

        void FacilityBuild(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            ProductionWindow newWindow = new ProductionWindow(NetClientManager.GetAvailableUnits("Facility"), selectedSystem);
        }
        void FacilityCancel(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            selectedSystem.ClearFacilityBuild();
            RefreshData();
        }

        void TroopBuild(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            ProductionWindow newWindow = new ProductionWindow(NetClientManager.GetAvailableUnits("Troop"), selectedSystem);
        }
        void TroopCancel(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            selectedSystem.ClearTroopBuild();
            RefreshData();
        }

        void ShipBuild(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            ProductionWindow newWindow = new ProductionWindow(NetClientManager.GetAvailableUnits("Ship"), selectedSystem);
        }
        void ShipCancel(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            selectedSystem.ClearShipBuild();
            RefreshData();
        }

        public void RefreshData()
        {
            shipbuild = 0; troopbuild = 0; facbuild = 0;

            foreach (var temp in selectedSystem.lFacilities)
            {
                if ((NetClientManager.UnitList[temp.GetReferenceID] as Facility).eType == FacilityType.Ships)
                    shipbuild++;
                if ((NetClientManager.UnitList[temp.GetReferenceID] as Facility).eType == FacilityType.Troops)
                    troopbuild++;
                if ((NetClientManager.UnitList[temp.GetReferenceID] as Facility).eType == FacilityType.Facilities)
                    facbuild++;
            }

            if (selectedSystem.FacilityBuildQuantity != 0)
            {
                lblFacilityDestination.Text = NetClientManager.gameUniverse.dUniverse[selectedSystem.FacilityBuildDestinationID].sName;
                lblFacilityNumber.Text = selectedSystem.FacilityBuildQuantity.ToString();
                progbarFacilityCurrentBuild.Range = NetClientManager.UnitList[selectedSystem.FacilityBuildReferenceID].iConstructionCost;
                progbarFacilityCurrentBuild.Value = selectedSystem.FacilityBuildCurrentProgress;
                lblNameFacilityBuild.Text = NetClientManager.UnitList[selectedSystem.FacilityBuildReferenceID].sName;
                progbarFacilityCurrentBuild.Refresh();
                string fac = NetClientManager.UnitList[selectedSystem.FacilityBuildReferenceID].sTextureName;
                imgFacilityCurrentBuild.Image = TextureManager.GetTexture(fac).BaseTexture as Texture2D;
            }
            else
            {
                lblFacilityDestination.Text = "";
                lblFacilityNumber.Text = "0";
                progbarFacilityCurrentBuild.Range = 1;
                progbarFacilityCurrentBuild.Value = 0;
                lblNameFacilityBuild.Text = "No Facilities Being Built";
                progbarFacilityCurrentBuild.Refresh();
                imgFacilityCurrentBuild.Image = TextureManager.GetTexture("Clear").BaseTexture as Texture2D;
            }

            if (selectedSystem.TroopBuildQuantity != 0)
            {
                lblDestinationTroopTrained.Text = NetClientManager.gameUniverse.dUniverse[selectedSystem.TroopBuildDestinationID].sName;
                progbarTroopBuild.Range = NetClientManager.UnitList[selectedSystem.TroopBuildReferenceID].iConstructionCost;
                progbarTroopBuild.Value = selectedSystem.TroopBuildCurrentProgress;
                lblNameTroopTrained.Text = NetClientManager.UnitList[selectedSystem.TroopBuildReferenceID].sName;
                lblNumTroopsTrained.Text = selectedSystem.TroopBuildQuantity.ToString();
                progbarTroopBuild.Refresh();
                string troop = NetClientManager.UnitList[selectedSystem.TroopBuildReferenceID].sTextureName;
                imgTroopBuild.Image = TextureManager.GetTexture(troop).BaseTexture as Texture2D;
            
            }
            else
            {
                lblDestinationTroopTrained.Text = "";
                lblNumTroopsTrained.Text = "0";
                progbarTroopBuild.Range = 1;
                progbarTroopBuild.Value = 0;
                lblNameTroopTrained.Text = "No Troops Being Trained";
                progbarTroopBuild.Refresh();
                imgTroopBuild.Image = TextureManager.GetTexture("Clear").BaseTexture as Texture2D;
            }

            if (selectedSystem.ShipBuildQuantity != 0)
            {
                lblNameOfBuild.Text = NetClientManager.UnitList[selectedSystem.ShipBuildReferenceID].sName;
                progbarShipBuild.Range = NetClientManager.UnitList[selectedSystem.ShipBuildReferenceID].iConstructionCost;
                progbarShipBuild.Value = selectedSystem.ShipBuildCurrentProgress;
                lblDestinationShipBuild.Text = NetClientManager.gameUniverse.dUniverse[selectedSystem.ShipBuildDestinationID].sName;
                lblNumShipsBuild.Text = selectedSystem.ShipBuildQuantity.ToString();
                progbarShipBuild.Refresh();
                string ship = NetClientManager.UnitList[selectedSystem.ShipBuildReferenceID].sTextureName;
                imgShipBuild.Image = TextureManager.GetTexture(ship).BaseTexture as Texture2D;
            }
            else
            {
                lblDestinationShipBuild.Text = "";
                lblNumShipsBuild.Text = "0";
                progbarShipBuild.Range = 1;
                progbarShipBuild.Value = 0;
                lblNameOfBuild.Text = "No Ships Being Built";
                progbarShipBuild.Refresh();
                imgShipBuild.Image = TextureManager.GetTexture("Clear").BaseTexture as Texture2D;
            }

        }

        void tabControl1_PageChanged(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                UnitPanel.GroupPanel.Visible = false;
            }
            else
            {
                UnitPanel.GroupPanel.Visible = true;
            }
        }
    }
}
